package com.neusoft.lohas.modules.shop.utils;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.google.common.collect.Lists;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.CookieUtils;
import com.neusoft.lohas.common.utils.SpringContextHolder;
import com.neusoft.lohas.modules.ivm.dao.merchant.MerchantDao;
import com.neusoft.lohas.modules.ivm.dao.product.ProductClassifyDao;
import com.neusoft.lohas.modules.ivm.dao.product.ProductDao;
import com.neusoft.lohas.modules.ivm.dao.product.ProductUnitDao;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;
import com.neusoft.lohas.modules.ivm.entity.product.ProductClassify;
import com.neusoft.lohas.modules.ivm.entity.product.ProductUnit;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

public class ShopUtils extends BaseService{

	private static MerchantDao merchantDao = SpringContextHolder.getBean(MerchantDao.class);
	private static ProductDao productDao = SpringContextHolder.getBean(ProductDao.class);
	private static ProductClassifyDao productClassifyDao = SpringContextHolder.getBean(ProductClassifyDao.class);
	private static ProductUnitDao productUnitDao = SpringContextHolder.getBean(ProductUnitDao.class);
	public static String COOKIE_SID = "sid";
	public static String COOKIE_MOBILE = "mobile";
	public static String COOKIE_CLASSIFYID = "choiceClassifyId";
	/**
	 * 获取当前商户信息
	 * @param id
	 * @return
	 */
	public static Merchant getMerchantById(String sid){
		if(StringUtils.isNotBlank(sid)) { //如果sid不为空
			return merchantDao.get(sid);
		}
		return null;
	}
	
	/**
	 * 获取商品的一级分类列表
	 * @return
	 */
	public static List<ProductClassify> getClassifyOneList(String sid){
		if(StringUtils.isNotBlank(sid)) { //如果sid不为空
			DetachedCriteria dc = productClassifyDao.createDetachedCriteria();
			dc.add(Restrictions.eq("delFlag", ProductClassify.DEL_FLAG_NORMAL));
			dc.add(Restrictions.eq("merchant.id", sid));
			dc.add(Restrictions.eq("level", "1"));
			dc.addOrder(Order.asc("sort"));
			return productClassifyDao.find(dc);
		}
		return Lists.newArrayList();
	}
	
	/**
	 * 获取二级分类，根据一级分类查询
	 * @param parentId
	 * @return
	 */
	public static List<ProductClassify> getClassifyTwoList(String sid, String parentId){
		if(StringUtils.isNotBlank(sid) && StringUtils.isNotBlank(parentId)) { //如果sid不为空
			DetachedCriteria dc = productClassifyDao.createDetachedCriteria();
			dc.add(Restrictions.eq("delFlag", ProductClassify.DEL_FLAG_NORMAL));
			dc.add(Restrictions.eq("merchant.id", sid));
			dc.add(Restrictions.eq("parent.id", parentId));
			dc.addOrder(Order.asc("sort"));
			return productClassifyDao.find(dc);
		}
		return Lists.newArrayList();
	}
	
	/**
	 * 获取当前商户所有的单位集合
	 * @return
	 */
	public static List<ProductUnit> getUnitList(String sid){
		if(StringUtils.isNotBlank(sid)) { //如果sid不为空
			DetachedCriteria dc = productUnitDao.createDetachedCriteria();
			dc.add(Restrictions.eq("delFlag", ProductClassify.DEL_FLAG_NORMAL));
			dc.add(Restrictions.eq("merchant.id", sid));
			dc.addOrder(Order.asc("id"));
			return productUnitDao.find(dc);
		}
		return Lists.newArrayList();
	}
	
	/**
	 * 获取商户编号session缓存值
	 * @param request
	 * @return
	 */
	public static String getCookieSid(HttpServletRequest request){
		return CookieUtils.getCookie(request, COOKIE_SID);
	}
	
	/**
	 * 获取手机号session缓存值
	 * @param request
	 * @return
	 */
	public static String getCookieMobile(HttpServletRequest request){
		return CookieUtils.getCookie(request, COOKIE_MOBILE);
	}
	
	/**
	 * 根据商户编号获取当前商户之前选中的商品分类
	 * @param sid
	 * @return
	 */
	public static String getCookieClassifyId(HttpServletRequest request, String sid, String classifyId){
		String cookie_classifyId = CookieUtils.getCookie(request, COOKIE_CLASSIFYID);
		if (StringUtils.isBlank(cookie_classifyId)) {
			return null;
		}else {
			if (StringUtils.isNotBlank(classifyId)) {
				if (cookie_classifyId.contains(sid+","+classifyId)) {
					if (cookie_classifyId.split(",").length==2) {
						return cookie_classifyId.split(",")[1];
					}else if(cookie_classifyId.split(",").length==3){
						return cookie_classifyId.split(",")[2];
					}
				}
			}else {
				if (cookie_classifyId.contains(sid)) {
					return cookie_classifyId.split(",")[1];
				}
			}
			return null;
		}
	}
}
